WARNING: This program has a bug, which means we need to fix it! This bug is a logic error.
RULE: Collision events take two parameters, sprite and hit_sprite. The sprite refers to the sprite connected to the event handler. The hit_sprite becomes whatever the main sprite hits.
Click Run and move the mouse to use the bat to hit a ball. Oops! The ball should disappear, not the bat!
Fix the code so.remove_sprite() removes hit_sprite (the ball) instead of sprite ( the bat) on line 9!
To navigate the page using the TAB key, first press ESC to exit the code editor.
stage.set_background("baseballfield")
stage.disable_floor()
sprite = codesters.Sprite("baseballbat")
sprite.set_size(.5)
timer = 5
def collision(sprite, hit_sprite):
sprite.turn_right(360)
stage.remove_sprite(sprite)
# add any other actions...
sprite.event_collision(collision)
### Don't touch any code below this! ###
### The error occurs above ###
def interval():
global timer
timer -= 1
x = random.randint(-250, 250)
ball = codesters.Sprite("baseball", x, 260)
ball.set_y_speed(random.randint(-15, -10))
# end the program after 5 seconds
if timer == 0:
stage.remove_all_events()
# add any other actions...
stage.event_interval(interval, 1)
def mouse_move():
x = stage.mouse_x()
y = stage.mouse_y()
sprite.set_position(x, y)
# add other actions...
stage.event_mouse_move(mouse_move)
t = codesters.Teacher()
func = t.find_text('collision')
call = t.find_function('event_collision')
removes = t.find_text('remove_sprite')
remove_params = t.get_parameters_for_function('remove_sprite')
try:
def_line = int(func[0][0])
def_line_indent = t.get_indent_at_line(def_line)
except:
def_line = -1
def_line_indent = -1
try:
call_line = int(call[0][0])
call_line_indent = t.get_indent_at_line(call_line)
except:
call_line = -1
call_line_indent = -1
try:
tval4a = int(removes[0][0])
tval4b = t.get_indent_at_line(tval4a)
tval4c = remove_params[0][0]
except:
tval4a = -1
tval4b = -1
tval4c = "DNE"
event_check = TestObjective()
event_check.add_success(len(func) == 2 and def_line != -1 and def_line_indent == 0 and call_line_indent == 0, "Great job!")
event_check.add_failure(def_line == -1, "Oops! Did you delete your Collision event?")
event_check.add_failure(def_line_indent > 0 or call_line_indent > 0, "Oops! Make sure your Collision event is NOT indented inside another event.")
t3 = TestObjective()
t3.add_success(tval4a != -1 and tval4b == 4 and tval4c == 'hit_sprite' and def_line <tval4a < call_line, "Great job!")
t3.add_failure(tval4a == -1, "Did you add Remove Sprite to your collision event?")
t3.add_failure(tval4a != -1 and tval4c != "hit_sprite", "Did you change sprite to hit_sprite in .remove_sprite?")
t3.add_failure(tval4b < 4, "Make sure your .remove_sprite() command is indented 4 spaces in your collision event.")
t3.add_failure(tval4b > 4, "Make sure your .remove_sprite() command is indented only 4 spaces inside your collision event.")
t3.add_failure(not (def_line <tval4a < call_line), "Make sure your .remove_sprite() command is inside your collision event.")
tester = TestManager()
tester.add_test_list([event_check, t3])
tester.run_tests()
tester.display_first_feedback()
Are you already running a Codesters project in another tab or window?
Micro:bit can only connect to one web page at a time.
Try stopping other Codesters projects or closing
other tabs or windows that may be using your Micro:bit.
If that doesn't fix the problem try disconnecting your Micro:bit,
reloading this page, and reconnecting your Micro:bit.